import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:study_flutter/yzhg/project/service/service_method.dart';
import 'package:study_flutter/yzhg/project/shop/YHttpDataType.dart';
import 'package:study_flutter/yzhg/project/shop/pages/inherited/HotGoodInherited.dart';

class HotGoods extends StatefulWidget {
  @override
  _HotGoodsState createState() => _HotGoodsState();
}

class _HotGoodsState extends State<HotGoods> {
  /*热销产品数组*/
  List<Map> hotGoodsList = [];

  /*上拉加载的状态*/
  int isFinishLoad = YHttpType.DATA_SUCCESS;

  @override
  void initState() {
    super.initState();
    _getHotGoods("1");
  }

  void _getHotGoods(String page) {
    getHomePageBelowContent(page.toString()).then((val) {
      var data = json.decode(val.toString());
      List<Map> newGoodsList = (data['data'] as List).cast();
      setState(() {
        hotGoodsList.addAll(newGoodsList);
        isFinishLoad = YHttpType.DATA_SUCCESS;
      });
    });
  }

  @override
  Widget build(BuildContext context) {

    var page = HotGoodInherited.of(context).page.toString();
    print("page的值===================" + page);
    _getHotGoods(page);

    return HotGoodInherited(
      page: 0,
      isFinishLoad: isFinishLoad,
      child: Container(
        child: Column(
          children: <Widget>[hotTitle, _wrapList()],
        ),
      ),
    );
  }

  Widget hotTitle = Container(
    margin: EdgeInsets.only(top: 10.0, bottom: 10.0),
    alignment: Alignment.center,
    color: Colors.transparent,
    child: Text(
      "火爆专区",
      style: TextStyle(fontSize: 16, color: Colors.blue),
    ),
  );

  Widget _wrapList() {
    if (hotGoodsList.length != 0) {
      List<Widget> listWidget = hotGoodsList.map((val) {
        return InkWell(
          onTap: () {},
          child: Container(
            width: ScreenUtil().setWidth(370),
            color: Colors.white,
            padding: EdgeInsets.all(5.0),
            margin: EdgeInsets.only(bottom: 3.0),
            child: Column(
              children: <Widget>[
                Image.network(val['image'], width: ScreenUtil().setWidth(368)),
                Text(val['name'],
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                        color: Colors.pink,
                        fontSize: ScreenUtil().setSp(26.0))),
                Row(
                  children: <Widget>[
                    Text("￥${val['mallPrice']}"),
                    Text(
                      "￥${val['price']}",
                      style: TextStyle(
                          color: Colors.black26,
                          decoration: TextDecoration.lineThrough),
                    )
                  ],
                )
              ],
            ),
          ),
        );
      }).toList();
      return Wrap(
        spacing: 2,
        children: listWidget,
      );
    } else {
      return Text("没有数据");
    }
  }
}
